package com.codedancer.campus.client.api.chatMessage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.chatMessage.ro.ChatMessageListPageSearchRo;
import com.codedancer.campus.client.api.chatMessage.ro.ChatMessagePageSearchRo;
import com.codedancer.campus.client.service.IChatMessageService;
import com.codedancer.campus.client.service.bo.chatMessage.ChatMessageListPageSearchBo;
import com.codedancer.campus.client.service.bo.chatMessage.ChatMessagePageSearchBo;
import com.codedancer.campus.client.service.vo.chatMessage.ChatRoomListVo;
import com.codedancer.campus.client.service.vo.chatMessage.ChatMessageVo;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "聊天信息Api", description = "聊天信息Api", tags = "聊天信息Api")
@RequestMapping("/api/chat-message")
public class ChatMessageApi extends BaseApi {

    @Autowired
    private IChatMessageService chatMessageService;

    @GetMapping("/page/history")
    @ApiOperation("分页查询历史聊天记录")
    public ApiResponse<IPage<ChatMessageVo>> page(
        @ApiParam(value = "聊天信息分页查询Ro", required = false)
            ChatMessagePageSearchRo chatMessagePageSearchRo) {
        ChatMessagePageSearchBo chatMessagePageSearchBo = BeanUtil.copyProperties(
            chatMessagePageSearchRo, ChatMessagePageSearchBo.class);
        chatMessagePageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(chatMessageService.page(chatMessagePageSearchBo));
    }

    @GetMapping("/page/list")
    @ApiOperation("分页查询聊天列表")
    public ApiResponse<IPage<ChatRoomListVo>> pageList(
        @ApiParam(value = "聊天列表分页查询Ro", required = false)
            ChatMessageListPageSearchRo chatMessageListPageSearchRo) {
        ChatMessageListPageSearchBo chatMessageListPageSearchBo = BeanUtil.copyProperties(
            chatMessageListPageSearchRo, ChatMessageListPageSearchBo.class);
        chatMessageListPageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(chatMessageService.pageList(chatMessageListPageSearchBo));
    }

    @GetMapping("/is-exits-no-read")
    @ApiOperation("是否存在未读消息")
    public ApiResponse<Boolean> isExitsNoRead(){
        return success(chatMessageService.isExitsNoRead(currentLoginUuid()));
    }
}
